package com.pachong.android.frameworkbase.utils;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;

import java.util.ArrayList;
import java.util.List;

/**
 * 判断sd卡的工具类
 * 
 * @author ocean
 * @contact :404583721@qq.com
 * @date 2014年11月18日
 */
public class MultiCardUtils {
	private static final String TAG = "MultiCardUtils";

	/**
	 * 外置SD卡路径
	 */
	private String mExternalSDCardPath = "";

	/**
	 * 内置SD卡路径
	 */
	private List<String> mInternalSDCardPath = new ArrayList<String>();

	/**
	 * 手机本身存储空间路径
	 */
	private String mPhoneDataPath = "";

	private static MultiCardUtils instance;

	private MultiCardUtils() {

	}

	/**
	 * 获得单实例接口
	 */
	public static MultiCardUtils getInstance(Context context) {
		if (instance == null) {
			instance = new MultiCardUtils();
			// 外置SD卡路径
			instance.mExternalSDCardPath = Environment
					.getExternalStorageDirectory().getAbsolutePath();
			// 手机本身存储空间路径
			instance.mPhoneDataPath = Environment.getDataDirectory().getPath();
			// context.getFilesDir().getAbsolutePath();
		}
		return instance;
	}

	/**
	 * 外置和内置SD卡是否存在
	 * 
	 * @return
	 */
	public boolean isSDCardExist() {
		return isExternalSDCardExist() || isInternalSDCardExist();
	}

	/**
	 * 内置存储卡是否存在
	 * 
	 * @return
	 */
	public boolean isInternalSDCardExist() {
		boolean bExist = false;
		bExist = mInternalSDCardPath != null && mInternalSDCardPath.size() > 0;
		// 存在,并且空间大于0
		return bExist && getInternalSDCardSpace() > 0;
	}

	/**
	 * 外置存储卡是否存在
	 * 
	 * @return
	 */
	public boolean isExternalSDCardExist() {
		boolean bExist = false;
		bExist = Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
		// 存在,并且空间大于0
		return bExist && getExternalSDCardSpace() > 0;
	}

	/**
	 * 获取外置存储卡剩余空间
	 * 
	 * @return
	 */
	public long getExternalSDCardSpace() {
		return getResidualSpace(mExternalSDCardPath);
	}

	/**
	 * 获取外置存储卡总空间
	 * 
	 * @return
	 */
	public long getExternalSDCardTotalSpace() {
		return getTotalSpace(mExternalSDCardPath);
	}

	/**
	 * 获取内置存储卡剩余空间
	 * 
	 * @return
	 */
	public long getInternalSDCardSpace() {
		// // if (mInternalSDCardPath != null && mInternalSDCardPath.size() > 0)
		// {
		// long size = 0;
		// for (String path : mInternalSDCardPath) {
		// long space = getResidualSpace(path);
		// if (space > size) {
		// size = space;
		// }
		// }
		//
		// return size;
		// // } else {
		// // return 0;
		// // }
		return getResidualSpace(mPhoneDataPath);
	}

	/**
	 * 获取内置存储卡总空间
	 * 
	 * @return
	 */
	public long getInternalSDCardTotalSpace() {
		// // if (mInternalSDCardPath != null && mInternalSDCardPath.size() > 0)
		// {
		// long size = 0;
		// // for (String path : mInternalSDCardPath) {
		// long space = getTotalSpace(path);
		// if (space > size) {
		// size = space;
		// }
		// // }
		//
		// return size;
		// // } else {
		// // return 0;
		// // }
		return getTotalSpace(mPhoneDataPath);
	}

	/**
	 * 获取目录总空间
	 * 
	 * @param directoryPath
	 * @return
	 */
	private long getTotalSpace(String directoryPath) {
		try {
			// 一个包装类，用来检索文件系统的信息
			StatFs sf = new StatFs(directoryPath);
			// 文件系统的块的大小（byte）
			long blockSize = sf.getBlockSize();
			// 文件系统的总的块数
			long totalBlocks = sf.getBlockCount();
			long totalSize = totalBlocks * blockSize;
			return totalSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 获取目录剩余空间
	 * 
	 * @param directoryPath
	 * @return
	 */
	private long getResidualSpace(String directoryPath) {
		try {
			StatFs sf = new StatFs(directoryPath);
			long blockSize = sf.getBlockSize();
			long availCount = sf.getAvailableBlocks();
			long availCountByte = availCount * blockSize;
			return availCountByte;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

}
